﻿using Newtonsoft.Json;
using QskjLibrary;
using QskjServiceKafka.Dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QskjServiceKafka.Service
{
    public static class CarDataUpdate
    {
        /// <summary>
        /// 锐明出租车基础数据
        /// </summary>
        public static void UpdateCarData()
        {
            try
            {
                //string dtStrPre = DateTime.Now.AddDays(-2).ToString("yyyyMMdd");
                //string sqlPre = $@"drop table if exists qskjrecord.B_TaxiRM{dtStrPre}";
                //MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sqlPre);
                string dtStr = DateTime.Now.ToString("yyyyMMdd"); 
                if (DateTime.Now.Hour < 7)
                {
                    string exists = $@"SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'qskjrecord' AND TABLE_NAME = 'B_TaxiRM{dtStr}';";
                    object resExi = MySqlHelper.ExecuteScalar(CommandType.Text, exists);
                    if (resExi == null || resExi == DBNull.Value)
                    {
                        string sql = $@"create table IF NOT EXISTS qskjrecord.B_TaxiRM{dtStr}
                        (
                           id                   bigint not null auto_increment,
                           companyId            int,
                           companyName          varchar(50),
                           carNo                varchar(50),
                           carTypeName          varchar(50),
                           vehicleIdentificationNumber varchar(50),
                           color                varchar(50),
                           mdtId                varchar(50),
                           mdt_id               int,
                           mdt_mdtid            varchar(50),
                           mdt_phoneNo          varchar(50),
                           mdt_iccid            varchar(50),
                           mdt_channelNo        int,
                           car_num                varchar(50),
                           primary key (id)
                        );";
                        MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql);
                    }
                }
                //读取数据
                var str = HttpHelper.Get().Result;
                List<TaxiInfoRuiMingDto> obj = JsonConvert.DeserializeObject<List<TaxiInfoRuiMingDto>>(str);
                foreach (var item in obj)
                {
                    if (item.mdt == null)
                    {
                        item.mdt = new Mdt();
                    }
                    string insertSql = $@"insert into qskjrecord.B_TaxiRM{dtStr}(companyId,companyName,carNo,carTypeName,vehicleIdentificationNumber,color,mdtId,mdt_id,mdt_mdtid,mdt_phoneNo,mdt_iccid,mdt_channelNo,car_num)
                                        values({item.companyId},'{item.companyName}','{item.carNo}','{item.carTypeName}','{item.vehicleIdentificationNumber}','{item.color}','{item.mdtId}',
                                                {item.mdt.id},'{item.mdt.mdtid}','{item.mdt.phoneNo}','{item.mdt.iccid}',{item.mdt.channelNo},'{item.carNo.Substring(1)}')";
                    MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, insertSql);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("读取出租车信息异常");
            }
        }
    }
}
